<?php

namespace app\forms;

use app\models\Item;
use app\components\validators\Exist;
use app\components\helpers\MappingHelper;
use app\components\validators\PositiveInteger;

/**
 * 权限项表单类
 */
class ItemForm extends Model
{
    public $id;
    public $parentId;
    public $name;
    public $route;
    public $dataAuth;
    public $status;
    public $remark;

    public $page;
    public $pageSize;

    public $userId;
    public $roleId;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],

            ['parentId', 'integer'],
            ['parentId', 'default', 'value' => 0],

            ['name', 'trim'],
            ['name', 'string', 'max' => 32],

            ['route', 'trim'],
            ['route', 'string', 'max' => 32],

            ['dataAuth', 'in', 'range' => array_column(Item::getDataAuthMapping(), 'id')],
            ['dataAuth', 'default', 'value' => Item::DATA_AUTH_NO],

            ['status', 'in', 'range' => array_column(MappingHelper::statusList(), 'id')],
            ['status', 'default', 'value' => MappingHelper::STATUS_ACTIVE],

            ['remark', 'trim'],
            ['remark', 'string', 'max' => 64],
            ['remark', 'default', 'value' => ''],

            ['page', PositiveInteger::class],
            ['page', 'default', 'value' => 1],

            ['pageSize', PositiveInteger::class],
            ['pageSize', 'default', 'value' => 10],

            ['userId', 'each', 'rule' => [PositiveInteger::class]],
            ['roleId', 'each', 'rule' => [PositiveInteger::class]],

            ['id', 'required', 'on' => ['update', 'delete', 'status', 'get-user', 'get-role']],
            [
                'id',
                'exist',
                'targetClass' => Item::class,
                'targetAttribute' => 'id',
                'on' => ['update', 'delete', 'status', 'get-user', 'get-role']
            ],

            [
                'parentId',
                Exist::class,
                'targetClass' => Item::class,
                'targetAttribute' => 'id',
                'on' => ['index', 'create']
            ],

            ['name', 'required', 'on' => ['create', 'update']],
            ['route', 'required', 'on' => ['create', 'update']],
        ];
    }


    public function scenarios()
    {
        return [
            'create' => ['parentId', 'name', 'route', 'status', 'dataAuth', 'remark'],
            'update' => ['id', 'name', 'route', 'status', 'dataAuth', 'remark'],
            'get-user' => ['id', 'name', 'page', 'pageSize'],
            'get-role' => ['id', 'name', 'page', 'pageSize'],
            'remove-user' => ['id', 'userId'],
            'remove-role' => ['id', 'roleId'],
            'status' => ['id', 'status'],
            'index' => ['parentId'],
            'delete' => ['id']
        ];
    }
}
